const { getParkings ,
     addParking ,
     amendState , 
     deleteParkings, 
     searchParkings, 
     checkParkingsState,
     getmyorders,
     modifyParkings} = require('../dao/parkingsDao');
module.exports.getParkings = async function (val) {
    //根据参数进行判断

    //进入if，则表示options有值，说明在获取部分车位，查询车位
    if (val.options) {
        const data = await searchParkings(val);
        return {
            message: '车位数据查询成功',
            status: 1,
            data
        }
    } else {
        //进入else,则表示options没有值，说明只是在获取车位
        try {
            const data = await getParkings(val);
            return {
                message: '车位数据获取成功',
                status: 1,
                data
            }
        } catch (error) {

            return {
                message: '车位数据获取失败',
                status: 0

            }
        }
    }
}

//删除车位信息
module.exports.deleteParkings = async function (val) {
    //接收到前端发送的值去数据库查看state状态值再确定是否删除
    const data = await checkParkingsState(val);
    const state = data[0].state
    if (state == 4) {
        return {
            message: '删除失败，该车辆已上架，请下架后删除',
            status: 0,
            data
        }
    } else if (state == 5) {
        return {
            message: '删除失败，该车位已出租，无法删除',
            status: 0,
            data
        }
    } else if (state == 1 || state == 2 || state == 3) {
        const data = await deleteParkings(val);
        if (data.nModified) {
            return {
                message: '删除成功',
                status: 1,
                data
            }
        } else {
            return {
                message: '删除失败',
                status: 0,
                data
            }
        }
    }
}

//验证用户信息可否修改checkCanChangeModify
module.exports.checkCanChangeModify = async function (val) {
    const data = await checkParkingsState(val);
    const state = data[0].state

    if (state == 4) {
        return {
            message: '该车辆已上架，你不能修改该车位信息，请下架后再修改',
            status: 0,
            data
        }
    } else if (state == 5) {
        return {
            message: '该车位已出租，无法修改该信息',
            status: 0,
            data
        }
    } else if (state == 2) {
        return {
            message: '该车位审核已通过，修改后将重新审核',
            status: 0,
            data
        }
    } else if (state == 6) {
        return {
            message: '该车位正在审核中，无法修改',
            status: 0,
            data
        }
    }

    else if (state == 1 || state == 3) {
        if (state) {
            return {
                message: '请输入你想修改的信息',
                status: 1,
                data
            }
        } else {
            return {
                message: '修改信息获取失败',
                status: 0,
                data
            }
        }
    }
}


//modifyParkings修改数据

module.exports.modifyParkings = async function (val) {
    const data = await modifyParkings(val);

    if (data.nModified) {
        return {
            data: {
                message: '修改成功',
                status: 1
            }
        }
    } else {
        return {
            data: {
                message: '修改失败',
                status: 0
            }
        }
    }
}
//添加车位 冰
module.exports.addParking = async function (val) {
    const data = await addParking(val);
    if (data._id) {
        return {
            data: {
                message: '添加成功',
                status: 1
            }
        }
    }
}

//修改上下架的state 冰
module.exports.amendState = async function (val) {
    var num = 0;
    if (val.state == (1 || 3)) {
        num = 6;
    } else if (val.state == 2) {
        num = 4;
    } else if (val.state == 4) {
        num = 2;
    };
    await amendState(val, num);
}

